package com.travel.server.manager.impl.command.article;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.article.QueryArticleClassListRequest;
import com.travel.server.manager.dto.response.article.QueryArticleClassListResponse;
import com.travel.server.manager.dto.response.article.QueryArticleClassListResponse.ArticleClassItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ArticleClassDal;
import com.travel.server.manager.impl.dal.PageDto;
import com.travel.server.manager.model.ArticleClassInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 16:14
 * @Desc:
 */
@Component
public class QueryArticleClassListCommand extends AbstractCommand<QueryArticleClassListRequest, QueryArticleClassListResponse> {

    @Autowired
    private ArticleClassDal classDal;

    @Override
    protected void checkInput(QueryArticleClassListRequest request) {
        checkPageInput(request);
    }

    @Override
    public QueryArticleClassListResponse handle(QueryArticleClassListRequest request) {
        checkInput(request);
        QueryArticleClassListResponse response = new QueryArticleClassListResponse();
        List<ArticleClassItem> articleList = new ArrayList<>();
        PageDto<ArticleClassInfo> pageDto = classDal.selectByPage(request.getOffset(), request.getLimit());
        response.setTotalNum(pageDto.getTotal());
        List<ArticleClassInfo> list = pageDto.getList();
        if(!CollectionUtil.isEmpty(list)){
            for(ArticleClassInfo info : list){
                ArticleClassItem item = new ArticleClassItem();
                item.setId(info.getId());
                item.setName(info.getClassName());
                item.setSort(info.getSort());
                articleList.add(item);
            }
        }
        response.setArticleClassList(articleList);
        return response;
    }
}
